<html>
<head><meta charset="utf-8"><title>Safety guarantees of InPlaceIterable · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Safety.20guarantees.20of.20InPlaceIterable.html">Safety guarantees of InPlaceIterable</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223743336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Safety%20guarantees%20of%20InPlaceIterable/near/223743336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Safety.20guarantees.20of.20InPlaceIterable.html#223743336">(Jan 23 2021 at 10:44)</a>:</h4>
<p>By implementing <code>InPlaceIterable</code> the implementor guarantees that a successful call to <code>next()</code> will have taken at least an item from the underlying <code>SourceIter</code>. However does this also apply to <code>try_fold</code>? I.e. If I call <code>try_fold</code> on an <code>InPlaceIterable</code> and it returns a failure do I have the same guarantees as if <code>next()</code> was called?  </p>
<p>My reasoning is the following:</p>
<ul>
<li>It returns a failure, which can only be provided by the closure since it requires an instance of the <code>Try::Error</code> type which the <code>try_fold</code> implementation knows</li>
<li>Since the closure was called, an item must have been supplied to it, which <strong>ideally</strong> requires calling <code>next()</code> or something equivalent. The implementation of <code>SpecFromIter</code> for <code>I: Iterator + SourceItemMarker</code>seems to assume this (see <a href="https://github.com/rust-lang/rust/blob/master/library/alloc/src/vec/source_iter_marker.rs#L60-L62">here</a>).</li>
</ul>



<a name="224544360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Safety%20guarantees%20of%20InPlaceIterable/near/224544360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Safety.20guarantees.20of.20InPlaceIterable.html#224544360">(Jan 29 2021 at 22:27)</a>:</h4>
<p>Yes, it should also apply  to try_fold. If try_fold yielded an element then at least one slot in the source must be available.<br>
If you have concrete changes in mind you can CC me on that PR since I am responsible for that hideous thing. :D<br>
It's only meant for std-internal use so far, so its spec may not be 100% watertight.</p>



<a name="224564297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Safety%20guarantees%20of%20InPlaceIterable/near/224564297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Safety.20guarantees.20of.20InPlaceIterable.html#224564297">(Jan 30 2021 at 04:10)</a>:</h4>
<p>There's not really any way that <code>try_fold</code> could fail without reading an item even if it wanted to -- there's no way to produce the required type without passing an item from the iterator.  (Well, if you know that the item type is <code>()</code> or something you could make one yourself, but I can't imagine any way to accidentally return an error from <code>try_fold</code> without reading at least one item.)</p>
<p>There are plenty of easy ways to do <code>try_fold</code> wrong, but that one, at least, isn't one of them <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="224582052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Safety%20guarantees%20of%20InPlaceIterable/near/224582052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Safety.20guarantees.20of.20InPlaceIterable.html#224582052">(Jan 30 2021 at 12:03)</a>:</h4>
<p>Well, technically someone could use specialization to clone and return the same value from <code>try_fold</code>, which would mean that only the first call will result in a call to <code>next</code>. Yes, this is pretty convoluted, but it's not explicitly forbidden. </p>
<p>Anyway, I was thinking if it was correct to implement <code>InPlaceIterable</code> for <code>ResultShunt</code> (which is internal to the stdlib), and since its <code>Iterator::next</code> implementation end up calling <code>try_fold</code> on the underlying iterator I ended up with this question.</p>



<a name="224584946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Safety%20guarantees%20of%20InPlaceIterable/near/224584946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Safety.20guarantees.20of.20InPlaceIterable.html#224584946">(Jan 30 2021 at 13:23)</a>:</h4>
<p>For <code>.collect::&lt;Result&lt;Vec&lt;_&gt;,_&gt;&gt;()</code>? Yeah that should work.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>